//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.smartstate.dataManagement.util;

import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;
import java.util.Date;

public class DateTimeUtil {
    public DateTimeUtil() {
    }

    public static String localDate2String(LocalDate localDate) {
        return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static LocalDate string2LocalDate(String fullDate) {
        return LocalDate.parse(fullDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static String localDateTime2String(LocalDateTime localDateTime) {
        return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static LocalDateTime string2LocalDateTime(String fullDateTime) {
        return LocalDateTime.parse(fullDateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static LocalDateTime date2LocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("+08"));
    }

    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.of("+08")).toInstant());
    }

    public static LocalDateTime epochMilli2LocalDateTime(long epochMilli) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.of("+08"));
    }

    public static long localDateTime2EpochMilli(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.of("+08")).toEpochMilli();
    }

    public static int weekOfYear(LocalDateTime localDateTime) {
        WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 1);
        return localDateTime.get(weekFields.weekOfYear());
    }

    public static LocalDate getFirstDayOfWeek(int year, int weekNum) {
        LocalDate firstDay = LocalDate.ofYearDay(year, 1);
        int dayNumOfYear = Math.max(weekNum * 7 - firstDay.getDayOfWeek().getValue() - 5, 1);
        return LocalDate.ofYearDay(year, dayNumOfYear);
    }

    public static LocalDate getLastDayOfWeek(int year, int weekNum) {
        LocalDate firstDay = LocalDate.ofYearDay(year, 1);
        int dayNumOfYear = Math.max(weekNum * 7 - firstDay.getDayOfWeek().getValue() + 1, 1);
        return LocalDate.ofYearDay(year, dayNumOfYear);
    }
}
